package com.freework.sys.dic.page.form.field;

import java.util.Collection;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.freework.util.LoadDirClass;

public class LoadFeildProperty {

	Map<String, List<FeildProperty>> map = new Hashtable<String, List<FeildProperty>>();
	LoadDirClass loadDirClass = null;

	public LoadFeildProperty() {
		load();
	}

	public static void main(String a[]) {
		LoadFeildProperty load=new LoadFeildProperty();
		System.out.println(load.getMap());
		
	}

	 public List<FeildProperty> getFeildPropertys(String fieldType){
		return map.get(fieldType);
		
	}
	 public Map<String, List<FeildProperty>> getMap(){
		 return map;
			
			
		}
	public void load() {
		Collection<FieldI> field = new LoadDirClass(FieldI.class).getObjects();
	
		for (FieldI field2 : field) {
			List<FeildProperty>  list=new LinkedList<FeildProperty> ();
			addFeildProperty(list,field2.getClass());
			map.put(field2.getFieldType(), list);
		}
	}
	
	public boolean contains(List<FeildProperty> list,String name){
		for (FeildProperty feildProperty : list) {
			if(feildProperty.getProperty().equals(name))
				return true;
		}
		return false;
	}

	public void addFeildProperty(List<FeildProperty> list,Class clazz){
		java.lang.reflect.Field[] fields =clazz.getDeclaredFields();
		
		Class superclass=clazz.getSuperclass();
		if(superclass!=null){
			addFeildProperty(list,superclass);
		}
		for (int i = 0; i < fields.length; i++) {
			
			String name=fields[i].getName();
			if(!contains(list,name)){
				String type=fields[i].getType().getSimpleName().toLowerCase();
				FeildCnName cnName=fields[i].getAnnotation(FeildCnName.class);
				if(cnName!=null){
					FeildProperty fp=new FeildProperty(name,cnName.value(),type);
					if(cnName.array()!=null&&cnName.array().length>0)
					fp.setArray(cnName.array());
					list.add(fp);
				}
			}
		
		}
		
		
	}
	

}
